﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using AnyOffice.Components;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.system.user
{
    public partial class create_key : ModulePage
    {
        public string uKey = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["dept_id"] == null) ? 0 : int.Parse(Request.QueryString["dept_id"].ToString());
                this.ViewState["DeptId"] = num;
                string str = (Request.QueryString["user_id"] == null) ? "" : Request.QueryString["user_id"].ToString();
                this.ViewState["UserId"] = str;
                string str2 = FormsAuthentication.HashPasswordForStoringInConfigFile(DateTime.Now.ToString(), "md5");
                this.uKey = str2;
                string commandText = "UPDATE [dbo].[Users] SET [UKey] = @UKey WHERE [UserId] = @UserId ";
                SqlParameter[] commandParameters = new SqlParameter[2];
                commandParameters[0] = new SqlParameter("UKey", SqlDbType.NVarChar, 32);
                commandParameters[0].Value = str2;
                commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
                commandParameters[1].Value = str;
                SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, commandText, commandParameters);
            }
            if (this.Page.IsPostBack)
            {
                if (this.UGuid.Value != string.Empty)
                {
                    string str4 = "UPDATE [dbo].[Users] SET [UGuid] = @UGuid  WHERE [UserId] = @UserId UPDATE [dbo].[Users] SET [UGuid]=NULL,[UKey]=NULL  WHERE  [UGuid] = @UGuid AND [UserId] <> @UserId";
                    SqlParameter[] parameterArray2 = new SqlParameter[2];
                    parameterArray2[0] = new SqlParameter("@UGuid", SqlDbType.NVarChar, 50);
                    parameterArray2[0].Value = this.UGuid.Value;
                    parameterArray2[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
                    parameterArray2[1].Value = this.ViewState["UserId"].ToString();
                    SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, str4, parameterArray2);
                }
                base.Conn.Close();
                Response.Redirect("user_dept.aspx?dept_id=" + this.ViewState["DeptId"].ToString() + "&usbkey=true");
            }
        }
    }
}
